package com.day46.test;

import com.day46.mapper.UserMapper;
import com.day46.utils.MybatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * @Author 咕唧
 * @Date 2022/5/13 21:43
 * @Version 1.0
 */
public class DemoTest1 {
    @Test
    public void findUser() throws Exception{
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.findUser();
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void findOne() {
        UserMapper userMapper = MybatisUtils.getMapper(UserMapper.class);

        User user = userMapper.findById(12);
        System.out.println(user);
    }

    @Test
    public void findUserByAge() {
        UserMapper userMapper = MybatisUtils.getMapper(UserMapper.class);

        List<User> userList = userMapper.findUserById(12);
        for (User user : userList) {
            System.out.println(user);
        }
    }
}
